<?php

namespace app\admin\model\house;

use app\admin\model\Admin;
use app\admin\model\Githouse;
use app\admin\model\Preproduct;
use think\Model;

class In extends Model
{
    // 表名
    protected $name = 'house_in';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'type_text',
        'inputtime_text'
    ];
    public function getOrderId(){

        $order_id = "IN-".date("Y")."CG-R-".date("md")."-".substr(time(),3).random(6,'0123456789');

        $info = $this->where(array('order_id' => $order_id))->find();

        if ($info) {
            $this->getOrderId();
        } else {
            return $order_id;
        }

    }



    public function getTypeList()

    {

        //1=采购入库,2=要货入库,3=退货入库,4=代理商退货回总部,5=调拔入库,6=盘盈,7=入库,其他
        return ['1' =>"采购入库",'2' =>"要货入库",'3' =>"退货入库",'4' => "代理商退货回总部",'5' =>"调拔入库",'6' =>"盘盈",'7' => "其他"];
    }
    public function getCidAttr($value){
        $db = new Githouse();
        $info = $db->where(array('id'=>$value))->field('gname')->find();
        return $info['gname'];
    }
    public function getShopidAttr($value){
        $db = new Preproduct();
        $info = $db->where(array('id'=>$value))->field('cname')->find();
        return $info['cname'];
    }
    public function getPublisherAttr($value){
        $db = new Admin();
        $info = $db->where(array('userid'=>$value))->field('username')->find();
        return $info['username'];
    }


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getInputtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['inputtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setInputtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
